package com.itheima.demo;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {
    private SqlSession session = null;
    private InputStream is = null;
    private UserMapper mapper = null;

    @Before
    public void before() throws IOException {
        //读取核心配置
        is = Resources.getResourceAsStream("sql.xml");
        //构建对象
        SqlSessionFactory f = new SqlSessionFactoryBuilder().build(is);
        //获取SqlSession对象
        session = f.openSession();
        //session获取接口的代理对象
        mapper = session.getMapper(UserMapper.class);
    }

    @After
    public void after() throws IOException {
        //6、事务提交
        session.commit();
        //7、释放资源
        session.close();
        is.close();
    }


    @Test
    public void testFindAll() throws Exception {
        //调用接口中的方法
        List<User> list = mapper.findAll();
        System.out.println(list);
    }

    @Test
    public void textFindName() {
        List<User> list = mapper.findName(" ");
        System.out.println(list);
    }

    @Test
    public void textFindName2() {
        List list = new ArrayList();
        list.add(1);
        mapper.findName2(list);
        System.out.println(list);
    }

    @Test
    public void textFindName3() {
        String[] str = {"a"};
        List<User> list = mapper.findName3(str);
        System.out.println(list);
    }

    @Test
    public void textDeleteID() {
        Integer[] id = {1, 2, 3};
        mapper.deleteID(null);
    }
}
